package com.sm.utils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.FileSystemNotFoundException;
import java.util.*;

public class PropertiesUtil {

    public static Map loadSqlFile(String filePath) {

        HashMap<String,String> map = new HashMap<String, String>();

        if (null == filePath || "".equals(filePath.trim())) {
            System.out.println("The file path is null,return");
            throw new FileSystemNotFoundException("file not found");
        }

        filePath = filePath.trim();

        // 获取资源文件
        InputStream is = PropertiesUtil.class.getClassLoader().getResourceAsStream(filePath);

        // 属性列表
        Properties prop = new Properties();

        try {
            // 从输入流中读取属性列表
            prop.load(is);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 返回Properties中包含的key-value的Set视图
        Set<Map.Entry<Object, Object>> set = prop.entrySet();
        // 返回在此Set中的元素上进行迭代的迭代器
        Iterator<Map.Entry<Object, Object>> it = set.iterator();
        String key = null, value = null;
        // 循环取出key-value
        while (it.hasNext()) {

            Map.Entry<Object, Object> entry = it.next();

            key = String.valueOf(entry.getKey());
            value = String.valueOf(entry.getValue());

            key = key == null ? key : key.trim().toUpperCase();
            value = value == null ? value : value.trim().toUpperCase();

            System.out.println("key:"+key+" value:"+value);

            map.put(key,value);
        }
        return map;
    }
}
